package com.example.android2019.homework.homework5;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.android2019.R;

public class ShopMarketActivity extends AppCompatActivity {
    private ListView listView;
    private int[] img = {R.drawable.zhuozi, R.drawable.pingguo, R.drawable.dangao, R.drawable.xianyi, R.drawable.damanvzhuang, R.drawable.nick, R.drawable.aj1, R.drawable.aj11, R.drawable.aj111, R.drawable.aj1111};
    private String[] names = {"桌子", "苹果", "蛋糕", "线衣", "大码女装", "耐克", "aj1", "aj11", "aj111", "aj1111"};
    private String[] prices = {"价格：1800元", "价格：10元/kg", "价格：300元", "价格：350元", "价格：1000元", "价格：1000元", "价格：1000元", "价格：1000元", "价格：1000元", "价格：1000元"};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_shop_market);

        listView = findViewById(R.id.shop_market_item_list);
        final ShopMarketAdapter adapter = new ShopMarketAdapter();
        listView.setAdapter(adapter);
        /*AdapterView.OnItemClickListener listener = new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                String s = adapter.names[position];
                String s1 = adapter.prices[position];
                Toast.makeText(ShopMarketActivity.this, s, Toast.LENGTH_SHORT).show();
                Toast.makeText(ShopMarketActivity.this, s1, Toast.LENGTH_SHORT).show();
            }
        };
        listView.setOnItemClickListener(listener);*/
    }

    class ShopMarketAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            return names.length;
        }

        @Override
        public Object getItem(int position) {
            return names[position];
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder viewHolder = new ViewHolder();
            if (convertView == null) {
                convertView = View.inflate(ShopMarketActivity.this, R.layout.activity_shop_market_item, null);
                viewHolder.imageView = convertView.findViewById(R.id.shop_market_item_image);
                viewHolder.textView1 = convertView.findViewById(R.id.shop_market_item_tv1);
                viewHolder.textView2 = convertView.findViewById(R.id.shop_market_item_tv2);
                convertView.setTag(viewHolder);
            } else {
                viewHolder = (ViewHolder) convertView.getTag();
            }
            viewHolder.imageView.setImageResource(img[position]);
            viewHolder.textView1.setText(names[position]);
            viewHolder.textView2.setText(prices[position]);
            return convertView;
        }
    }

    class ViewHolder {
        ImageView imageView;
        TextView textView1;
        TextView textView2;
    }
}
